import UIKit
import MMarkUI
import SnapKit

class MarkdownDetailViewController: UIViewController {
    
    // MARK: - Properties
    private let markdownView: MaakuMarkContainerView = {
        let view = MaakuMarkContainerView()
        return view
    }()

    private let sourceTextView: UITextView = {
        let textView = UITextView()
        textView.font = .monospacedSystemFont(ofSize: 14, weight: .regular)
        textView.backgroundColor = .secondarySystemBackground
        textView.isEditable = false
        textView.textContainerInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
        textView.layer.cornerRadius = 12
        textView.clipsToBounds = true
        return textView
    }()
    
    private let modeLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 12, weight: .medium)
        label.textColor = .secondaryLabel
        label.textAlignment = .center
        label.alpha = 0
        return label
    }()
    
    private let content: String
    private var isPreviewMode: Bool = true
    
    private lazy var previewButton: UIBarButtonItem = {
        let button = UIBarButtonItem(
            image: UIImage(systemName: "doc.text.fill"),
            style: .plain,
            target: self,
            action: #selector(togglePreviewMode)
        )
        return button
    }()
    
    // MARK: - Initialization
    init(title: String, content: String) {
        
        self.content = content
        super.init(nibName: nil, bundle: nil)
        self.title = title
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupContent()
    }
    
    // MARK: - Setup
    private func setupUI() {
        navigationItem.title = title
        navigationItem.rightBarButtonItem = previewButton
        view.backgroundColor = .systemBackground
        
        view.addSubview(markdownView)
        view.addSubview(sourceTextView)
        view.addSubview(modeLabel)
        
        markdownView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide).inset(UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
        }
        
        sourceTextView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide).inset(UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
        }
        
        modeLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide).offset(8)
        }
        
        updateViewVisibility()
    }
    
    private func setupContent() {
        sourceTextView.text = content
        markdownView.render(markdown: content)
    }
    
    // MARK: - Actions
    @objc private func togglePreviewMode() {
        isPreviewMode.toggle()
        
        // 使用动画切换视图
        UIView.animate(withDuration: 0.3, animations: {
            self.updateViewVisibility()
            self.showModeSwitchHint()
        })
        
        updatePreviewButton()
        
        // 触觉反馈
        let generator = UIImpactFeedbackGenerator(style: .medium)
        generator.impactOccurred()
    }
    
    // MARK: - Private Methods
    private func updateViewVisibility() {
        markdownView.alpha = isPreviewMode ? 1 : 0
        sourceTextView.alpha = isPreviewMode ? 0 : 1
    }
    
    private func showModeSwitchHint() {
        modeLabel.text = isPreviewMode ? "切换到预览模式" : "切换到源码模式"
        modeLabel.alpha = 1
        
        // 2秒后淡出提示
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            UIView.animate(withDuration: 0.3) {
                self.modeLabel.alpha = 0
            }
        }
    }
    
    private func updatePreviewButton() {
        let imageName = isPreviewMode ? "doc.text.fill" : "doc.richtext"
        let title = isPreviewMode ? "查看原文" : "预览效果"
        previewButton.image = UIImage(systemName: imageName)
        previewButton.accessibilityLabel = title
    }
}
